import QtQuick 1.1
import com.nokia.meego 1.0
import "Logic.js" as Logic
import cppWiFiMonitor 1.0

Page {
    id: pageWiFiMonitor
    anchors.margins: 3
    orientationLock: appWindow.lockOrientation()

    property string curr_iface_monitor: cppSelectedIfaceMonitor

    property string indicatorEssid: ""//cppEssid0
    property string indicatorMac: ""//cppMac0
    property string indicatorFreq: ""//cppFreq0
    property string indicatorChannel: ""//cppChannel0
    property string indicatorPic: ""//cppEncPic0

    onVisibleChanged: {
        if(visible) {
            monitorWiFi.updateWiFiData()
        }
    }

    function openIndicator() {
        if ( monitorWiFi.isWork ) {
            scanStartStopButton.iconId = "toolbar-mediacontrol-play"
            monitorWiFi.timerStop()
            tiUpdateWiFiData.enabled = true
        }
        Logic.openFile("WiFiIndicator.qml");
    }

    tools: ToolBarLayout {
        visible: true
        ToolIcon { iconId: "toolbar-back"; onClicked: { pageStack.pop(); } }
        ToolIcon {
            id: scanStartStopButton
            iconId: "toolbar-mediacontrol-play"
            smooth: true
            onClicked: {
                startWiFiMonitor()
            }
        }
        ToolIcon {
            id: tiUpdateWiFiData
            iconId: tiUpdateWiFiData.enabled ? "toolbar-refresh" : "toolbar-refresh-dimmed"
            states: [
                State {
                    name: "rotated"
                    PropertyChanges { target: tiUpdateWiFiData; rotation: 180; }
                },
                State {
                    name: "rotated_back"
                    PropertyChanges { target: tiUpdateWiFiData; rotation: 0; }
                }
            ]
            transitions: Transition { RotationAnimation {duration: 700; direction: RotationAnimation.Counterclockwise} }
            onClicked: {
                if ( (tiUpdateWiFiData.state == "rotated_back") || (tiUpdateWiFiData.state == "") ) {
                    tiUpdateWiFiData.state = "rotated"
                } else {
                    tiUpdateWiFiData.state = "rotated_back"
                }
                monitorWiFi.updateWiFiData()
            }
        }
        /*ToolIcon {
            id: imgGear
            iconId: "toolbar-settings"
            onClicked: {
            }
        }*/
        ToolIcon {
            id: tiWiFiDataView
            enabled: false
            iconId: "toolbar-search-dimmed"
            onClicked: {
                if( indicatorEssid != "" ) {
                    openIndicator()
                }
            }
        }
        ToolIcon {
            id: tiListWiFiData
            iconId: "toolbar-view-menu"
            onClicked: wifiListData()
         }
    }

    /* запуск/остановка автоматического обновления данных */
    function startWiFiMonitor( ) {
        if ( monitorWiFi.isWork === false ) {
            scanStartStopButton.iconId = "toolbar-mediacontrol-stop"
            monitorWiFi.timerStart()
            tiUpdateWiFiData.enabled = false
        } else {
            scanStartStopButton.iconId = "toolbar-mediacontrol-play"
            monitorWiFi.timerStop()
            tiUpdateWiFiData.enabled = true
        }
    }

    Rectangle {
        id: indicatorUpdate
        z: 20
        anchors.top: parent.top
        anchors.topMargin: 12
        anchors.right: parent.right
        anchors.rightMargin: 10
        height: 18
        width: 18
        radius: 9
        opacity: 0.7
        //color: "white"
        color: "transparent"
        visible: false
        Rectangle {
            anchors.centerIn: parent
            width: 14
            height: 14
            radius: 7
            //visible: indicatorUpdate.visible
            opacity: 1
            color: "yellow"
            visible: false
        }
        Image {
            id: imgBlow
            source: "pictures/blow.png"
            anchors.centerIn: parent
        }
    }

    /***********************************************************************/
    Rectangle {
        id: paneGraph
        anchors.fill: parent
        anchors.centerIn: parent
        radius: 10
        color: "black"
        Behavior on height { NumberAnimation { duration: 350; } }
        WiFiMonitor {
            id: monitorWiFi
            width: parent.width
            height: parent.height
            anchors.centerIn: parent.centerIn
            //onChangeValueParam: changeValueParamOnPane()
            onUpdateStatusChange: {
                if( monitorWiFi.isWork === false ) { //автоматическое обновление включено
                    if( monitorWiFi.isUpdate ) {
                        tiUpdateWiFiData.enabled = false
                        indicatorUpdate.visible = true
                    } else {
                        /* обновление закончено */
                        tiUpdateWiFiData.enabled = true
                        indicatorUpdate.visible = false
                        cppWiFiData.initListWiFiData()
                    }
                } else { //обновление вручную
                    if( monitorWiFi.isUpdate ) {
                        indicatorUpdate.visible = true
                    } else {
                        /* обновление закончено */
                        indicatorUpdate.visible = false
                        cppWiFiData.initListWiFiData()
                    }
                }
            } //onUpdateStatusChange
        }
    }

    /***********************************************************************/

    QueryDialog {
        id: dlgAboutWiFi
        message: qsTr("This item is in development. New features will be available in new versions. If you have any comments, please write to <qnetworkmanager@gmail.com>.")
    }

    //=========================
    Component {
        id: itemDelegate
        Rectangle {
            id: wrapper
            clip: true
            width: listViewWiFiData.width
            height: 100
            radius: 10
            gradient: Gradient {
                GradientStop { position: 0; color: mytheme.grColor2 }
                GradientStop { position: 1; color: mytheme.grColor3 }
            }
            border.color: mytheme.pageTitleAreaColor
            Image {
                id: imgWiFi
                source: wifiEncriptPic
                anchors.left: parent.left
                anchors.leftMargin: 10
                anchors.topMargin: 3
                anchors.top: parent.top
            }
            Text {
                id: txtEncyptName
                text: wifiEncrypt
                color: mytheme.fontMainColor
                anchors.top: imgWiFi.bottom
                anchors.horizontalCenter: imgWiFi.horizontalCenter
                font.pointSize: 10
            }
            Item {
                id: txtEssid
                anchors.top: parent.top
                anchors.left: imgWiFi.right
                anchors.leftMargin: 15
                height: parent.height/3
                width: parent.width - imgWiFi.width - 20
                clip: true
                Text {
                    anchors.verticalCenter: parent.verticalCenter
                    text: wifiEssid
                    color: mytheme.fontAltColor
                    font.pointSize: 18
                    font.bold: true
                }
            }
            Item {
                id: txtMac
                anchors.top: txtEssid.bottom
                anchors.left: imgWiFi.right
                anchors.leftMargin: 15
                height: parent.height/3
                width: parent.width - imgWiFi.width - 30
                Text {
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left
                    text: wifiMac
                    color: mytheme.fontMainColor
                    font.pointSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 16 : 13
                }
                Text {
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 40 : 25
                    text: "("+(wifiChannelFreq*1000)+" MHz) Ch:" + wifiChannel
                    color: mytheme.fontMainColor
                    font.pointSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 16 : 13
                }
            }
            Item {
                id: txtLevel
                anchors.top: txtMac.bottom
                anchors.left: imgWiFi.right
                anchors.leftMargin: 15
                height: parent.height/3
                width: parent.width - imgWiFi.width
                Row {
                    height: parent.height
                    width: parent.width
                    anchors.centerIn: parent
                    Text {
                        anchors.verticalCenter: parent.verticalCenter
                        text: "-"+wifiLeveldBm + " dBm"
                        color: mytheme.fontMainColor
                        width: 0.22*parent.width
                        font.pointSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 16 : 14
                    }
                    MyProgressBar {
                        anchors.verticalCenter: parent.verticalCenter
                        width: parent.width*0.63
                        height: parent.height*0.6
                        minValue: 0
                        maxValue: 100
                        value: (wifiLevelPer * 100)
                        color: (value > 33) ? value < 66 ? "orange" : "green" : "red"
                        colorText: theme.inverted === false ? "black" : value > 55 ? mytheme.fontMainColorInverted : mytheme.fontMainColor
                    }
                }

            }


            states: State {
                name: "Current"
                when: ( (wrapper.ListView.isCurrentItem) && (indicatorEssid != "") )
                PropertyChanges { target: wrapper; border.color: "yellow" }
                PropertyChanges { target: wrapper; border.width: 4 }
            }

            property string curr_item: ""
            property string prev_item: ""

            transitions: Transition {
                NumberAnimation { properties: "border.width"; duration: curr_item === prev_item ? 0 : 200; onCompleted: {prev_item = curr_item} }
            }

            MouseArea {
                id: ma
                anchors.fill: parent
                onClicked: {
                    curr_item = wifiMac+wifiEssid
                    wrapper.ListView.view.currentIndex = index

                    indicatorEssid = wifiEssid
                    indicatorMac = wifiMac
                    indicatorChannel = wifiChannel
                    indicatorFreq = wifiChannelFreq
                    indicatorPic = wifiEncriptPic
                }
                onDoubleClicked: {
                    openIndicator()
                }
            }

        } //Rectangle
    }
    // ========== Component { id: itemDelegate ... }

    Rectangle {
        id: dlgWiFiData
        z: 10
        color: "transparent"
        //anchors.centerIn: parent
        //anchors.fill: parent
        height: parent.height
        width: parent.width
        opacity: 0.85
        visible: false
        ListView {
            id:listViewWiFiData
            anchors.horizontalCenter: parent.horizontalCenter
            width: parent.width*0.95
            height: parent.height
            model: cppListWiFiData
            delegate: itemDelegate
            clip: true
        }
    }
    function wifiListData()
    {
        if( !dlgWiFiData.visible ) {
            dlgWiFiData.visible = true
            tiListWiFiData.iconId = "toolbar-view-menu-dimmed"
            x_from = pageWiFiMonitor.width - 100
            x_to = 0
            y_from = pageWiFiMonitor.height
            y_to = 0
            s_from = 0.1
            s_to = 1
            animShowDlg.running = true;

            tiWiFiDataView.iconId = "toolbar-search"
            tiWiFiDataView.enabled = true
        } else {
            tiListWiFiData.iconId = "toolbar-view-menu"
            x_from = 0
            x_to = pageWiFiMonitor.width - 100
            y_from = 0
            y_to = pageWiFiMonitor.height
            s_from = 1
            s_to = 0.1
            animShowDlg.running = true;

            tiWiFiDataView.iconId = "toolbar-search-dimmed"
            tiWiFiDataView.enabled = false
        }
    }

    property int x_from: pageWiFiMonitor.width
    property int x_to: 0
    property int y_from: pageWiFiMonitor.height
    property int y_to: 0
    property real s_from: 0.1
    property real s_to: 1
    property int howLong: 400

    ParallelAnimation {
        id: animShowDlg
        running: false
        NumberAnimation {
            target: dlgWiFiData;
            property: "x";
            duration: howLong;
            from: x_from
            to: x_to
            easing.type: Easing.InOutQuad
        }
        NumberAnimation {
            target: dlgWiFiData;
            property: "y";
            duration: howLong;
            from: y_from
            to: y_to
            easing.type: Easing.InOutQuad
        }
        NumberAnimation {
            target: dlgWiFiData;
            property: "scale";
            duration: howLong;
            from: s_from
            to: s_to
            easing.type: Easing.InOutQuad
        }
        onRunningChanged: {
            //console.log("onRunningChanged: s_from="+s_from+" s_to="+s_to+" running="+running )
            if( (s_from > s_to) && !running ) {
                //console.log("visible set false")
                dlgWiFiData.visible = false
            }
        }
    }

}
